#导入模块
#code utf-8
import pygame,sys,math,traceback
from pygame.locals import *
import player
import bullet
import enemy
import supplies
import myfont
import icon
import blackhole

#======初始化======
pygame.init()
pygame.mixer.init()
width,height=pygame.display.list_modes()[0]#分辨率列表
screen=pygame.display.set_mode((width,height),RESIZABLE)
init_difficulty=1
pygame.display.set_caption('像素飞机大战')
player=player.Player(width,height)
smallenemyxx=pygame.sprite.Group()
midenemyxx=pygame.sprite.Group()
bigenemyxx=pygame.sprite.Group()
extra_enemyxx=pygame.sprite.Group()
enemyxx=pygame.sprite.Group()
chainxx=pygame.sprite.Group()
player_bulletxx=pygame.sprite.Group()
small_enemy_bulletxx=pygame.sprite.Group()
mid_enemy_bulletxx=pygame.sprite.Group()
missilexx=pygame.sprite.Group()
for i in range(3):#生成敌方飞机
    a=enemy.SmallEnemy(width,height)
    smallenemyxx.add(a)
    enemyxx.add(a)
for i in range(20):#生成锁链
    chain=enemy.BigEnemyChain(width,height)
    chainxx.add(chain)
for i in range(1):
    a=enemy.MidEnemy(width,height)
    midenemyxx.add(a)
    enemyxx.add(a)
for i in range(0):
    a=enemy.BigEnemy(width,height)
    bigenemyxx.add(a)
    enemyxx.add(a)
with open('data/highest_score.txt','rt') as f:
    init_string_score=f.read()
score_message=icon.ScoreMessage(width,height)
highest_score_message=icon.HighestScoreMessage(width,height,init_string_score)
myfont=myfont.MyFont()
double_bullet=supplies.DoubleBullet(width,height)
big_bullet=supplies.BigBullet(width,height)
missile=supplies.Missile(width,height)
narrow_power=supplies.NarrowPower(width,height)
volume_set_button=icon.VolumeSetButton(width,height)
volume_set_base=icon.VolumeSetBase(width,height)
pause_button=icon.PauseButton(width,height)
start_button=icon.StartButton(width,height)
mouse_icon=icon.MouseIcon(width,height)
start_option=icon.StartOption(width,height)
volume_set_option=icon.VolumeSetOption(width,height)
end_option=icon.EndOption(width,height)
replay_icon=icon.ReplayIcon(width,height)
exclamatory_mark=icon.ExclamatoryMark(width,height)
return_button=icon.ReturnButton(width,height)
difficulty_message=icon.DifficultyMessage(width,height,init_difficulty)
blackhole=blackhole.BlackHole(width,height)
#==========载入游戏音效==========
pygame.mixer.music.load("audio/bgm.mp3")
pygame.mixer.music.set_volume(1.2)
#======主程序======
def main():
    global screen
    delay=60000
    clock=pygame.time.Clock()
    fullscreen=0
    pygame.mixer.music.play(-1)
    game_caption=pygame.image.load('images/game_caption.png').convert_alpha()
    end_die=pygame.image.load('images/end_die.png').convert_alpha()
    double_bullet_supply_code=False
    big_bullet_supply_code=False
    narrow_power_supply_code=False
    missile_supply_code=False
    double_bullet_code=False
    big_bullet_code=False
    narrow_power_code=False
    chain_code=False
    pause_code=False
    running=False
    start_interface_code=True
    end_die_code=False
    pause_code=False
    mouse_down=False
    blackhole_list=[blackhole.image,blackhole.image2,blackhole.image3,blackhole.image4,blackhole.image5,blackhole.image6]
    while 1:
        #开始界面大循环
        while start_interface_code:
            if delay==0:
                delay=60000
            delay-=1
            mouse_pos=pygame.mouse.get_pos()
            mouse=pygame.mouse.get_pressed()
            if mouse[0]:
                if volume_set_button.rect.left<mouse_pos[0]<volume_set_button.rect.right and volume_set_button.rect.bottom>mouse_pos[1]>volume_set_button.rect.top:
                    mouse_down=True
                else:
                    if volume_set_base.rect.left<mouse_pos[0]<volume_set_base.rect.right and volume_set_base.rect.bottom>mouse_pos[1]>volume_set_base.rect.top:
                        mouse_down=True
            else:
                mouse_down=False
                
            for event in pygame.event.get():#事件捕捉
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 0<mouse_pos[0]<start_option.rect.width and start_option.rect.top<mouse_pos[1]<start_option.rect.bottom:
                        mouse_down=True
                    if 0<mouse_pos[0]<volume_set_option.rect.right and volume_set_option.rect.top<mouse_pos[1]<volume_set_option.rect.bottom:
                        mouse_down=True
                    if 0<mouse_pos[0]<end_option.rect.width and end_option.rect.top<mouse_pos[1]<end_option.rect.bottom:
                        mouse_down=True
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.VIDEORESIZE:
                    width,height=event.size[0],event.size[1]
                    screen=pygame.display.set_mode((width,height),RESIZABLE)
                if event.type == pygame.KEYDOWN:
                    if event.key == K_F11:
                        if fullscreen==0:
                            screen=pygame.display.set_mode((width,height),RESIZABLE|FULLSCREEN)
                            fullscreen=1
                        elif fullscreen==1:
                            screen=pygame.display.set_mode((width,height),RESIZABLE)
                            fullscreen=0
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
            screen.fill((255,255,255))
            mouse_icon.move(mouse_pos)#设置光标
            if(delay%3)==0:
                screen.blit(mouse_icon.image,mouse_icon.rect)
            elif(delay%3)==1:
                screen.blit(mouse_icon.image2,mouse_icon.rect)
            elif(delay%3)==2:
                screen.blit(mouse_icon.image3,mouse_icon.rect)
            screen.blit(game_caption,[500,0])

            screen.blit(highest_score_message.image,highest_score_message.rect)#绘制最高分选项
            if 0<mouse_pos[0]<start_option.rect.right and start_option.rect.top<mouse_pos[1]<start_option.rect.bottom:
                screen.blit(start_option.word_image,start_option.rect)#游戏开始选项
                if mouse_down:
                    delay=60000
                    double_bullet_supply_code=False
                    big_bullet_supply_code=False
                    missile_supply_code=False
                    double_bullet_code=False
                    big_bullet_code=False
                    chain_code=False
                    running=True
                    end_die_code=False
                    start_interface_code=False
                    for e in extra_enemyxx:
                        e.kill()
                    for i in player_bulletxx:
                        player_bulletxx.remove(i)
                    for e in enemyxx:
                        e.alive=False
                    for s in small_enemy_bulletxx:
                        small_enemy_bulletxx.remove(s)
                    for m in mid_enemy_bulletxx:
                        mid_enemy_bulletxx.remove(m)
                    player.__init__(width,height)
                    double_bullet.__init__(width,height)
                    big_bullet.__init__(width,height)
                    missile.__init__(width,height)
                    narrow_power.__init__(width,height)
                    score_message.__init__(width,height)
                    difficulty_message.__init__(width,height,init_difficulty)
                    volume_set_base.reset()
                    volume_set_button.reset()
                    mouse_down=False
            else:
                screen.blit(start_option.image,start_option.rect)
            
            if 0<mouse_pos[0]<volume_set_option.rect.width and volume_set_option.rect.top<mouse_pos[1]<volume_set_option.rect.bottom:
                screen.blit(volume_set_option.word_image,volume_set_option.rect)#设置音量选项
                if mouse_down:
                    if volume_set_button.switch:
                        volume_set_base.reset()
                        volume_set_button.reset()
                        volume_set_button.switch=False
                        mouse_down=False
                    else:
                        volume_set_base.arise()
                        volume_set_button.arise(volume_set_base.rect,volume_set_button.volume_value)
                        volume_set_button.switch=True
                        mouse_down=False
            else:
                screen.blit(volume_set_option.image,volume_set_option.rect)
            if volume_set_button.switch:
                screen.blit(volume_set_base.image,volume_set_base.rect)
                screen.blit(volume_set_button.image,volume_set_button.rect)
                if not(volume_set_button.rect.left<mouse_pos[0]<volume_set_button.rect.right and volume_set_button.rect.bottom>mouse_pos[1]>volume_set_button.rect.top):
                    if volume_set_base.rect.left<mouse_pos[0]<volume_set_base.rect.right and volume_set_base.rect.bottom>mouse_pos[1]>volume_set_base.rect.top:
                        if mouse_down:
                            volume_set_base.move(mouse_pos)
                            volume_set_button.arise(volume_set_base.rect,volume_set_button.volume_value)
                if volume_set_button.rect.left<mouse_pos[0]<volume_set_button.rect.right and volume_set_button.rect.bottom>mouse_pos[1]>volume_set_button.rect.top:
                    if mouse_down:
                        volume_set_button.move(volume_set_base.rect,mouse_pos)
                        pygame.mixer.music.set_volume(volume_set_button.volume_value)

            if 0<mouse_pos[0]<end_option.rect.right and end_option.rect.top<mouse_pos[1]<end_option.rect.bottom:
                screen.blit(end_option.word_image,end_option.rect)#退出游戏选项
                if mouse_down:
                    with open('data/highest_score.txt','w') as f:
                        f.writelines(highest_score_message.string_score)
                    pygame.quit()
                    sys.exit()
            else:
                screen.blit(end_option.image,end_option.rect)
            
            pygame.display.flip()
        #游戏进行大循环
        while running:
            highest_score_message.getHighestScore(str(score_message.num)) #获取最高分
            if delay==0:
                delay=60000
            delay-=1
            mouse_pos=pygame.mouse.get_pos()
            key=pygame.key.get_pressed()
            now_time=pygame.time.get_ticks()
            for i in smallenemyxx:
                if not(i.alive):
                    i.reset()
            for i in midenemyxx:
                if not(i.alive):
                    i.reset()
            for i in bigenemyxx:
                if not(i.alive):
                    i.reset()
                    for k in chainxx:
                        k.reset()
            if key[K_SPACE]:
                if (delay%5)==0:
                    if double_bullet_code and not(big_bullet_code):
                        player_bulletxx.add(bullet.SmallBullet(width,height,[player.rect.midtop[0]-3,player.rect.midtop[1]-2]))
                        player_bulletxx.add(bullet.SmallBullet(width,height,[player.rect.midtop[0]+32,player.rect.midtop[1]+30]))
                        player_bulletxx.add(bullet.SmallBullet(width,height,[player.rect.midtop[0]-40,player.rect.midtop[1]+30]))
                    elif big_bullet_code and not(double_bullet_code):
                        player_bulletxx.add(bullet.BigBullet(width,height,[player.rect.midtop[0]-3,player.rect.midtop[1]-2]))
                    elif big_bullet_code and double_bullet_code:
                        player_bulletxx.add(bullet.BigBullet(width,height,[player.rect.midtop[0]-3,player.rect.midtop[1]-2]))
                        player_bulletxx.add(bullet.BigBullet(width,height,[player.rect.midtop[0]+32,player.rect.midtop[1]+30]))
                        player_bulletxx.add(bullet.BigBullet(width,height,[player.rect.midtop[0]-40,player.rect.midtop[1]+30]))
                    else:#生成子弹
                        player_bulletxx.add(bullet.SmallBullet(width,height,[player.rect.midtop[0]-3,player.rect.midtop[1]-2]))
            
            if key[K_w] or key[K_UP]:#移动设置
                player.moveUp()
            if key[K_s] or key[K_DOWN]:
                player.moveDown()
            if key[K_a] or key[K_LEFT]:
                player.moveLeft()
            if key[K_d] or key[K_RIGHT]:
                player.moveRight()
            
            for event in pygame.event.get():#事件捕捉
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if width-104<mouse_pos[0]<width and 0<mouse_pos[1]<120:
                        mouse_down=True
                    if return_button.rect.left<mouse_pos[0]<return_button.rect.right and return_button.rect.top<mouse_pos[1]<return_button.rect.bottom:
                        mouse_down=True
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.VIDEORESIZE:
                    width,height=event.size[0],event.size[1]
                    screen=pygame.display.set_mode((width,height),RESIZABLE)
                if event.type == pygame.KEYDOWN:
                    if event.key == K_p:
                        running=False
                        pause_code=True
                    if event.key == K_q:
                        if missile.num>0:#生成导弹
                            angle=math.atan2(height/2-(player.rect.top+player.rect.height/2),width/2-(player.rect.left+player.rect.width/2))
                            missile.num-=1
                            missilexx.add(supplies.SendMissile(width,height,[player.rect.left+player.rect.width/2,player.rect.top+player.rect.height/2],angle))
                    if event.key == K_F11:
                        if fullscreen==0:
                            screen=pygame.display.set_mode((width,height),RESIZABLE|FULLSCREEN)
                            fullscreen=1
                        else:
                            screen=pygame.display.set_mode((width,height),RESIZABLE)
                            fullscreen=0
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
            screen.fill((255,255,255))
            for b in player_bulletxx:#绘制子弹
                b.move()
                screen.blit(b.image,b.rect)
                   #掩膜碰撞检测,返回被击中的小型敌机
                col_smallenemy = pygame.sprite.spritecollide(b,smallenemyxx,False,pygame.sprite.collide_mask)
                if col_smallenemy:#如果有被击中小型敌机
                    b.hitnum-=1
                    if b.hitnum<=0:
                        player_bulletxx.remove(b)
                    for c in col_smallenemy:
                        c.health-=1
                        if c.health <= 0:
                            if not(player.invincible_code):
                                score_message.num+=10
                            c.alive=False
                if b.rect.top<0:
                    player_bulletxx.remove(b)
                #掩膜碰撞检测,返回被击中的中型敌机
                col_midenemy = pygame.sprite.spritecollide(b,midenemyxx,False,pygame.sprite.collide_mask)
                if col_midenemy:#如果有被击中中型敌机
                    b.hitnum-=1
                    if b.hitnum<=0:
                        player_bulletxx.remove(b)
                    for c in col_midenemy:
                        c.health-=1
                        if c.health <= 0:
                            if not(player.invincible_code):
                                score_message.num+=50
                            c.alive=False
                if b.rect.top<0:
                    player_bulletxx.remove(b)
                #掩膜碰撞检测,返回被击中的大型敌机
                col_bigenemy = pygame.sprite.spritecollide(b,bigenemyxx,False,pygame.sprite.collide_mask)
                if col_bigenemy:#如果有被击中大型敌机
                    b.hitnum-=1
                    if b.hitnum<=0:
                        player_bulletxx.remove(b)
                    for c in col_bigenemy:
                        c.health-=1
                        if c.health <= 0:
                            if not(player.invincible_code):
                                score_message.num+=300
                            c.alive=False
                if b.rect.top<0:
                    player_bulletxx.remove(b)
                    
            if (delay%1000) == 0:#三发弹供应
                double_bullet_supply_code=True
            if double_bullet_supply_code:
                double_bullet.move()
                if (delay%2)==0:
                    screen.blit(double_bullet.image2,double_bullet.rect)
                else:
                    screen.blit(double_bullet.image,double_bullet.rect)
                if double_bullet.rect.top>height:
                    double_bullet.reset()
                    double_bullet_supply_code=False
                if pygame.sprite.collide_mask(player,double_bullet):
                    score_message.num+=100
                    double_bullet.reset()
                    double_bullet_supply_code=False
                    double_bullet_code=True
                    double_bullet_time=pygame.time.get_ticks()
            if double_bullet_code:
                if now_time-double_bullet_time>=20000:#设置三发弹时限
                    double_bullet_code=False
                double_time_message=myfont.getChineseFont('剩余'+'%.2f'%((20000-now_time+double_bullet_time)/1000)+'秒' ,(0,0,0))
                screen.blit(double_bullet.icon_image,[3*50,height-50])
                screen.blit(double_time_message,[4*50,height-50])
            else:
                double_time_close=myfont.getChineseFont('暂无该能力',(0,0,0))
                screen.blit(double_bullet.icon_image,[3*50,height-50])
                screen.blit(double_time_close,[4*50,height-50])

            if (delay%3000) == 0:#能量弹供应
                big_bullet_supply_code=True
            if big_bullet_supply_code:
                big_bullet.move()
                if (delay%2)==0:
                    screen.blit(big_bullet.image2,big_bullet.rect)
                else:
                    screen.blit(big_bullet.image,big_bullet.rect)
                if big_bullet.rect.top>width:
                    big_bullet_supply_code=False
                    big_bullet.reset()
                if pygame.sprite.collide_mask(player,big_bullet):
                    score_message.num+=100
                    big_bullet_supply_code=False
                    big_bullet.reset()
                    big_bullet_code=True
                    big_bullet_time=pygame.time.get_ticks()
            if big_bullet_code:
                if now_time-big_bullet_time>=10000:#设置能量弹时限
                    big_bullet_code=False
                big_time_message=myfont.getChineseFont('剩余'+'%.2f'%((10000-now_time+big_bullet_time)/1000)+'秒' ,(0,0,0))
                screen.blit(big_bullet.icon_image,[9*50,height-50])
                screen.blit(big_time_message,[10*50,height-50])
            else:
                big_time_close=myfont.getChineseFont('暂无该能力',(0,0,0))
                screen.blit(big_bullet.icon_image,[9*50,height-50])
                screen.blit(big_time_close,[10*50,height-50])

            change_code=narrow_power_code
            if (delay%2000) == 0:#缩小能力供应
                narrow_power_supply_code=True
            if narrow_power_supply_code:
                narrow_power.move()
                if (delay%2)==0:
                    screen.blit(narrow_power.image2,narrow_power.rect)
                else:
                    screen.blit(narrow_power.image,narrow_power.rect)
                if narrow_power.rect.top>width:
                    narrow_power_supply_code=False
                    narrow_power.reset()
                if pygame.sprite.collide_mask(player,narrow_power):
                    score_message.num+=100
                    narrow_power_supply_code=False
                    narrow_power.reset()
                    narrow_power_code=True
                    narrow_power_time=pygame.time.get_ticks()
            if narrow_power_code:
                if now_time-narrow_power_time>=10000:#设置缩小能力时限
                    narrow_power_code=False
                narrow_power_time_message=myfont.getChineseFont('剩余'+'%.2f'%((10000-now_time+narrow_power_time)/1000)+'秒' ,(0,0,0))
                screen.blit(narrow_power.icon_image,[15*50,height-50])
                screen.blit(narrow_power_time_message,[16*50,height-50])
            else:
                narrow_power_time_close=myfont.getChineseFont('暂无该能力',(0,0,0))
                screen.blit(narrow_power.icon_image,[15*50,height-50])
                screen.blit(narrow_power_time_close,[16*50,height-50])
            if change_code!=narrow_power_code:     #变换大小
                player.changeBody([player.rect.left,player.rect.top])
                
            if (delay%5000) == 0:#核弹供应
                missile_supply_code=True
            if missile_supply_code:
                missile.move()
                if (delay%2)==0:
                    screen.blit(missile.image,missile.rect)
                else:
                    screen.blit(missile.image2,missile.rect)
                if missile.rect.top>width:
                    missile_supply_code=False
                    missile.reset()
                if pygame.sprite.collide_mask(player,missile):
                    score_message.num+=100
                    missile_supply_code=False
                    missile.reset()
                    missile.num+=1
            if (delay%1000)==0:      #黑洞周期
                blackhole.alive=False
                blackhole.arise()
            if blackhole.alive:      #绘制黑洞     
                screen.blit(blackhole_list[(delay%6)],[blackhole.rect[0]-(blackhole_list[(delay%6)].get_rect().width/2-blackhole.rect.width/2),blackhole.rect[1]-(blackhole_list[(delay%6)].get_rect().height/2-blackhole.rect.height/2)])
                if pygame.sprite.collide_mask(blackhole,player):
                    player.alive=False
                col_enemy=pygame.sprite.spritecollide(blackhole,enemyxx,False,pygame.sprite.collide_mask)
                for c in col_enemy:
                    c.alive=False
                if now_time-blackhole.time>5000:
                    blackhole.reset()
                    blackhole.alive=False
            for e in smallenemyxx:#绘制小型敌机
                e.moveDown()
                e.moveTransverse()
                screen.blit(e.image,e.rect)
                if e.rect.bottom>0:
                    if (delay%60)==0:#产生小型敌机子弹
                        small_enemy_bulletxx.add(bullet.SmallEnemyBullet(width,height,[e.rect.midbottom[0]-5,e.rect.midbottom[1]]))
                if pygame.sprite.collide_mask(e,player):
                    player.alive=False
                    e.alive=False
            for i in small_enemy_bulletxx:#绘制小型敌机子弹
                i.move()
                screen.blit(i.image,i.rect)
                   #掩膜碰撞检测
                if pygame.sprite.collide_mask(i,player):
                    i.hitnum-=1
                    if i.hitnum<=0:
                        small_enemy_bulletxx.remove(i)
                    player.alive=False
                if i.rect.bottom>height:
                    small_enemy_bulletxx.remove(i)
            for e in midenemyxx:#绘制中型敌机
                e.moveDown()
                if e.rect.bottom>0:
                    if (delay%20)==0:#产生中型敌机子弹
                        small_enemy_bulletxx.add(bullet.MidEnemyBullet(width,height,[e.rect.midbottom[0]-5,e.rect.midbottom[1]]))
                if height//2-4<=e.rect.bottom<=height//2+4:
                    e.y_speed+=2
                if e.rect.bottom>=height//2-4:
                    if (delay%2)==0:
                        screen.blit(e.image2,e.rect)
                    else:
                        screen.blit(e.image3,e.rect)
                else:
                    screen.blit(e.image,e.rect)
                if pygame.sprite.collide_mask(e,player):
                    player.alive=False
                    e.alive=False
            for i in mid_enemy_bulletxx:#绘制中型敌机子弹
                i.move()
                screen.blit(i.image,i.rect)
                   #掩膜碰撞检测
                if pygame.sprite.collide_mask(i,player):
                    i.hitnum-=1
                    if i.hitnum<=0:
                        small_enemy_bulletxx.remove(i)
                    player.alive=False
                if i.rect.bottom>height:
                    mid_enemy_bulletxx.remove(i)
            for e in bigenemyxx:#绘制大型敌机
                e.moveDown()
                if e.rect.bottom>height//2-50:
                    screen.blit(e.chain2,[0,e.rect.top+3])
                    screen.blit(pygame.transform.rotate(e.chain2,180),[width-242,e.rect.top-6])
                    screen.blit(e.image2,e.rect)
                    k=e.produce_chain()
                    index=0
                    for c in chainxx:
                        if index<=len(k)-1:
                            c.rect.left,c.rect.top=k[index]
                            screen.blit(c.image,c.rect)#绘制铁索
                            index+=1
                else:
                    screen.blit(e.image,e.rect)
                if pygame.sprite.collide_mask(e,player):
                    player.alive=False
                    e.alive=False
                if pygame.sprite.spritecollide(player,chainxx,False,pygame.sprite.collide_mask):
                    player.alive=False
            for m in missilexx:                    #绘制导弹
                m.move()
                if height/2-10<m.rect.top+m.rect.height/2<height/2+10:
                    screen.blit(m.boom_image,m.rect)
                    for e in enemyxx:
                        e.alive=False
                    missilexx.remove(m)
                else:
                    if (delay%2) == 0:
                        screen.blit(m.image,m.rect)
                    else:
                        screen.blit(m.image2,m.rect)
            
            if player.alive:                      #生或死
                if player.invincible_code:#无敌状态
                    if (delay%2)==0:
                        screen.blit(player.image,player.rect)
                    else:
                        pass
                    if now_time-invincible_time>=3000:
                        player.invincible_code=False
                else:
                    if (delay%2)==0:
                        screen.blit(player.image,player.rect)
                    else:
                        screen.blit(player.image2,player.rect)
            else:
                if player.invincible_code:#无敌状态
                    if (delay%2)==0:
                        screen.blit(player.image,player.rect)
                    else:
                        pass
                    if now_time-invincible_time>=6000:
                        player.invincible_code=False
                else:
                    player.reset()
                    player.invincible_code=True
                    invincible_time=pygame.time.get_ticks()
                player.alive=True
            
            difficulty_message.change=difficulty_message.num                   #难度改变
            difficulty_message.getDifficulty()
            if difficulty_message.change!=difficulty_message.num:
                difficulty_message.changeDifficulty()
                a=enemy.SmallEnemy(width,height)
                extra_enemyxx.add(a)
                smallenemyxx.add(a)
                enemyxx.add(a)
                a=enemy.MidEnemy(width,height)
                extra_enemyxx.add(a)
                midenemyxx.add(a)
                enemyxx.add(a)
                a=enemy.BigEnemy(width,height)
                extra_enemyxx.add(a)
                bigenemyxx.add(a)
                enemyxx.add(a)
                for e in enemyxx:
                    e.levelUp()
            screen.blit(difficulty_message.image,difficulty_message.rect)

            score_message.changeScore(score_message.num)                       #得分信息
            screen.blit(score_message.image,score_message.rect)
            
            missile_message=myfont.getChineseFont("X"+str(missile.num),(0,0,0))#导弹信息
            screen.blit(missile_message,[50+5,height-50//2-18])
            screen.blit(missile.icon_image,[0,height-50])
            
            for i in range(1,player.health+1):
                screen.blit(player.health_image,[width-i*57,height-66])
            if player.health <=0:
                running=False
                end_die_code=True
            
            if pause_button.rect.left<mouse_pos[0]<pause_button.rect.right and pause_button.rect.top<mouse_pos[1]<pause_button.rect.bottom:#暂停按钮
                screen.blit(pause_button.word_image,pause_button.rect)
                if mouse_down:
                    running=False
                    pause_code=True
                    mouse_down=False
            else:
                screen.blit(pause_button.image,pause_button.rect)
            if return_button.rect.left<mouse_pos[0]<return_button.rect.right and return_button.rect.top<mouse_pos[1]<return_button.rect.bottom:
                screen.blit(return_button.word_image,return_button.rect)
                if mouse_down:
                    running=False
                    start_interface_code=True
                    mouse_down=False
            else:
                screen.blit(return_button.image,return_button.rect)
            pygame.display.flip()
            clock.tick(90)  #设置最高帧率为90帧每秒
        #暂停大循环
        while pause_code:
            if delay==0:
                delay=60000
            delay-=1
            mouse_pos=pygame.mouse.get_pos()
            for event in pygame.event.get():#事件捕捉
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if width-104<mouse_pos[0]<width and 0<mouse_pos[1]<120:
                        mouse_down=True
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.VIDEORESIZE:
                    width,height=event.size[0],event.size[1]
                    screen=pygame.display.set_mode((width,height),RESIZABLE)
                if event.type == pygame.KEYDOWN:
                    if event.key == K_F11:
                        if fullscreen==0:
                            screen=pygame.display.set_mode((width,height),RESIZABLE|FULLSCREEN)
                            fullscreen=1
                        elif fullscreen==1:
                            screen=pygame.display.set_mode((width,height),RESIZABLE)
                            fullscreen=0
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
            screen.fill((255,255,255))
            if width-start_button.rect.width<mouse_pos[0]<width and 0<mouse_pos[1]<start_button.rect.height:#开始按钮
                screen.blit(pause_button.interface_image,[0,0])
                screen.blit(start_button.word_image,start_button.rect)
                if mouse_down:
                    running=True
                    pause_code=False
                    mouse_down=False
            else:
                screen.blit(pause_button.interface_image,[0,0])
                screen.blit(start_button.image,start_button.rect)
            pygame.display.flip()
        #死亡大循环
        while end_die_code:
            if delay==0:
                delay=60000
            delay-=1
            mouse_pos=pygame.mouse.get_pos()
            for event in pygame.event.get():#事件捕捉
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if replay_icon.rect.left<mouse_pos[0]<replay_icon.rect.right and replay_icon.rect.top<mouse_pos[1]<replay_icon.rect.bottom:
                        mouse_down=True
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.VIDEORESIZE:
                    width,height=event.size[0],event.size[1]
                    screen=pygame.display.set_mode((width,height),RESIZABLE)
                if event.type == pygame.KEYDOWN:
                    if event.key == K_F11:
                        if fullscreen==0:
                            screen=pygame.display.set_mode((width,height),RESIZABLE|FULLSCREEN)
                            fullscreen=1
                        elif fullscreen==1:
                            screen=pygame.display.set_mode((width,height),RESIZABLE)
                            fullscreen=0
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
            screen.fill((255,255,255))
            screen.blit(end_die,[320,168])
            if (delay%100)==0:
                exclamatory_mark.move()
            screen.blit(exclamatory_mark.image,exclamatory_mark.rect)
            if replay_icon.rect.left<mouse_pos[0]<replay_icon.rect.right and replay_icon.rect.top<mouse_pos[1]<replay_icon.rect.right:
                screen.blit(replay_icon.word_image,[0,0])              #重玩按钮
                if mouse_down:
                    delay=60000
                    double_bullet_supply_code=False
                    big_bullet_supply_code=False
                    missile_supply_code=False
                    double_bullet_code=False
                    big_bullet_code=False
                    chain_code=False
                    running=True
                    end_die_code=False
                    start_interface_code=False
                    for e in extra_enemyxx:
                        e.kill()
                    for i in player_bulletxx:
                        player_bulletxx.remove(i)
                    for e in enemyxx:
                        e.alive=False
                    for s in small_enemy_bulletxx:
                        small_enemy_bulletxx.remove(s)
                    for m in mid_enemy_bulletxx:
                        mid_enemy_bulletxx.remove(m)
                    player.__init__(width,height)
                    missile.__init__(width,height)
                    score_message.__init__(width,height)
                    difficulty_message.__init__(width,height,init_difficulty)
                    mouse_down=False
            else:
                screen.blit(replay_icon.image,replay_icon.rect)
            pygame.display.flip()

#======程序入口======
if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()
